<?php

class WebglController extends Controller {
  public function __construct($id, $module = null) {
    parent::__construct($id, $module);
    $this -> layout = 'application.views.layouts.layout';
  }
  public function actionTest(){
    // $this -> layout = 'application.views.layouts.layout0429';
    $this -> render('main');
  }
  
  public function actionBuild(){
    // $menuId= json_decode($relFdRses=$_REQUEST['menuId']);
    // 
    // if($menuId=="choice_hs")
    
    $hsId= json_decode($_REQUEST['hsId']);
    $uhId= json_decode($_REQUEST['uhId']);
    if(isset($hsId)){
     // echo json_decode($hsId);
      
      $model=HouseStyles::model()->with('Supplier')->with('RoomStyles')->with('BuildingInfos')->findByPk($hsId);
      
      $objHS = new stdClass();
      $objHS->id=$model->id;
      $objHS->supplier=$model->Supplier->abbreviation;
      $objHS->description=$model->description;
      $objHS->thumbnailPath=$model->thumbnailPath;
      $objHS->bi_id=$model->bi_id;
      $objHS->hTypeId=$model->hTypeId;
      $objHS->area=$model->area;
      $objHS->houseName=$model->houseName;
      $objHS->address=$model->address;
      $objHS->user_id=$model->user_id;
      $objHS->inputDate=$model->inputDate;
      
      
   $aryRS=array();
    foreach( $model->RoomStyles as $rs){
      $objRS = new stdClass();
      $objRS->id=$rs->id;
      $objRS->hs_id=$rs->hs_id;
      $objRS->size_x=$rs->size_x;
      $objRS->size_y=$rs->size_y;
      $objRS->size_z=$rs->size_z;
      $objRS->position_x=$rs->position_x;
      $objRS->position_y=$rs->position_y;
      $objRS->position_z=$rs->position_z;
      $objRS->roomType=$rs->roomType;
      array_push($aryRS,$objRS);
    }
    
    $objHS->RoomStyles=$aryRS;
    
    $aryBI=array();
    foreach( $model->BuildingInfos as $bi){
      $objBI = new stdClass();
      $objBI->id=$bi->id;
      $objBI->hs_id=$bi->hs_id;
      $objBI->size_x=$bi->size_x;
      $objBI->size_y=$bi->size_y;
      $objBI->size_z=$bi->size_z;
      $objBI->position_x=$bi->position_x;
      $objBI->position_y=$bi->position_y;
      $objBI->position_z=$bi->position_z;
      $objBI->roomType=$bi->roomType;
      array_push($aryRS,$objBI);
    }    
    
    $objHS->BuildingInfos=$aryRS;
      
      $model2=FacilityRoom::model()->with('FacilityDetails')->findAll('uh_id=:uhId',array(':uhId'=>$uhId));
      $aryFR=array();
      
      foreach($model2 as $fr){
      $objFR = new stdClass();
      $objFR->id=$fr->id;
      $objFR->fd_id=$fr->rs_id;
      $objFR->rs_id=$fr->rs_id;
      $objFR->uh_id=$fr->uh_id;
      $objFR->position_x=$fr->position_x;
      $objFR->position_y=$fr->position_y;
      $objFR->position_z=$fr->position_z;
      $objFR->rotation_x=$fr->rotation_x;
      $objFR->rotation_y=$fr->rotation_y;
      $objFR->rotation_z=$fr->rotation_z;
      $objFR->size_x=$fr->size_x;
      $objFR->size_y=$fr->size_y;
      $objFR->size_z=$fr->size_z;
      $objFR->parasitizedFace= $fr->parasitizedFace;
      
      // $aryFD=array();
      // foreach( $model2->FacilityDetails as $fd){
      $fd=$fr->FacilityDetails;
      $objFD = new stdClass();
      $objFD->id=$fd->id;
      $objFD->gTypeId=$fd->gTypeId;
      $objFD->geometryPath=$fd->geometryPath;
      $objFD->name=$fd->name;
      $objFD->description=$fd->description;
      $objFD->thumbPath=$fd->thumbPath;
      $objFD->dependId=$fd->dependId;
      $objFD->inputDate=$fd->inputDate;
      $objFD->usable=$fd->usable;
      // array_push($aryFD,$objFD);
    // } 
      $objFR->FacilityDetails= $objFD;
      array_push($aryFR,$objFR);
      
      }

      
      $this->render('buildHouse',array('houseStyle'=>$objHS,'facilityRoom'=>$aryFR,'hsId'=>$hsId,'uhId'=>$uhId));
    }else{
      $this -> render('buildHouse');
    }
      
  }
  
  public function actionhome() {
    $this -> layout = 'application.views.layouts.layoutHome';
    $this -> render('home');
  }
  
  public function actionShowHouseList(){
    $usId= json_decode($_REQUEST['userId']);

    $record=UserHouse::model()->with('Users')->findAll(array(
    'condition'=>'user_id=:userId',
    'params'=>array(':userId'=>$usId),
    'order'=>'inputDate desc'
    ));
     // echo json_encode($houseList[0]->user->username); 
    $this->render('showHouseList',array('houseList'=>$record));
  }
  
  public function actionLoadHouse(){
    
  }
  
  public function actionSaveHouse(){
     $facis= json_decode($relFdRses=$_REQUEST['relFdRses']);
      echo json_encode($_REQUEST['uh_id']);
    
$model=FacilityRoom::model();
       $transaction=$model->dbConnection->beginTransaction();
       try{
         
         // $uh=new UserHouse;
         
         // $ucount=UserHouse::model()->count ("id=:uh_id",array(":uh_id"=>$_REQUEST['uh_id']));
         
         if(isset($_REQUEST['uh_id'])){
         $uh=UserHouse::model()->findByPk($_REQUEST['uh_id']);
         $uh->inputDate=date('Y-m-d H:i:s');
         echo json_encode($uh->inputDate);
         $uh->save();
         }else{
           $uh=new UserHouse;
           $uh->user_id=1;
           $uh->hs_id=$_REQUEST['hs_id'];
          // $uh->id=$_REQUEST['uh_id'];
           $uh->inputDate=date('Y-m-d H:i:s');
           $uh->save();
         }
         
         foreach($facis as $f){
          
          if(isset($f->frid)){
            $model=FacilityRoom::model()->findByPk($f->frid);
          }else{
            $model=new FacilityRoom; 
          }
           // $model=FacilityRoom::model()->findByPk(10);
           $model->fd_id=$f->fdId;
           $model->uh_id=$uh->id;
           $model->rs_id=$f->rsId;
           $model->position_x=round($f->fdPositionX);
           $model->position_y=round($f->fdPositionY);
           $model->position_z=round($f->fdPositionZ);
           $model->rotation_x=$f->fdRotationX;
           $model->rotation_y=$f->fdRotationY;
           $model->rotation_z=$f->fdRotationZ;
           $model->size_x=round($f->fdSizeX);
           $model->size_y=round($f->fdSizeY);
           $model->size_z=round($f->fdSizeZ);
           $model->parasitizedFace=$f->parasitizedFace;
           $model->save();           
         }
         $transaction->commit();
       }catch(Exception $e){ 
         echo $e;
         $transaction->rollBack();
       }  
          
  }
  
  public function actionShowStyles(){
  //  $this -> layout = 'application.views.layouts.none';
  //  $roomstyles=RoomStyles::model()->with('housestyles')->findAll();
  //  $this -> render('showHouseStyles',array('roomstyles'=>$roomstyles));
  	$this -> layout = 'application.views.layouts.none';
  	// $houseStyles=HouseStyles::model()->with('BuildingInfos')->findAll();
  	// echo json_encode($houseStyles);
  	 $houseStyles=HouseStyles::model()->with('Supplier')->with('RoomStyles')->with('BuildingInfos')->findAll();
  	
  	$this->render('showHouseStyles',array('houseStyles'=>$houseStyles));
  } 
  
  public function actionShowRoomStyles(){
   $houseId=json_decode($_REQUEST['house_id']);
    $roomStyles=RoomStyles::model()->getRecordByHouseId($houseId);
   // $roomStyles=RoomStyles::model()->findAll();
    
    //echo $_REQUEST['house_id'];
    echo json_encode($roomStyles);  
    
  }
  
  public function actionSaveStyle(){
      $rooms=json_decode($_REQUEST['rooms']);
      $house=json_decode($_REQUEST['house']); 
       $model=RoomStyles::model();
       $transaction=$model->dbConnection->beginTransaction();
       try{
         
        //  insert into house_styles;
       if(isset($house->id)){
            $model=HouseStyles::model()->findByPk($house->id);
          }else{
            $model=new HouseStyles; 
          }
        $hs=new HouseStyles;
        $hs->description=$house->desc;
        $hs->supplier_id=$house->supplyid;
        //$hs->building_name=$house->BuildName;
        $hs->bi_id=1;
        $hs->hTypeId=$house->hTypeId;
        $hs->area=$house->area;
        $hs->address=$house->address;
        $hs->user_id=1;
        $hs->save();


        /**
         * get id from house_styles;
         */
        foreach($rooms as $r){
          $model=new RoomStyles;
          $model->size_x=$r->size_x;
          $model->size_y=$r->size_z;
          $model->size_z=$r->size_y;
          $model->position_x=$r->position_x;
          $model->position_y=$r->position_z;
          $model->position_z=$r->position_y;
          $model->hs_id=$hs->id;
          // $model->rt_id=$r->type;
          $model->save();
        } 
        $transaction->commit();
       }catch(Exception $e){
         echo $e;
         $transaction->rollBack();
       }
       
       echo json_encode($model->attributes);
        // echo $model;
    // if(isset($_REQUEST['rooms'])){
    //   $model=RoomStyles::model(); 
    //   $transaction=$model->dbConnection->beginTransaction();
    //   try {
    //     foreach($rooms as $r){
    //       $model->size_x=$r->size_x;
    //       $model->size_y=$r->size_y;
    //       $model->size_y=$r->size_y;
    //       $model->position_x=$r->position_x;
    //       $model->position_y=$r->position_y;
    //       $model->position_z=$r->position_z;          
    //     }  
    //     $transaction->commit(); 
    //   }catch(Exception $e){
    //       $transaction->rollBack();
    //     }   
    // }
    

  }
 
 
  public function actionFitmentPanel(){
    $this -> renderPartial('fitmentPanel');
  }
  public function actionPopmenu(){
     $hostInfo=json_decode($_REQUEST['hostInfo']);
  
    if($hostInfo->name=="wall"){
      $url="panelStructure";
    }else if ($hostInfo->name== "structure"){
      $url="panelStructure";
    }else if($hostInfo->name=="houseInfo"){
     $url='panelHouseInfo'; 
    }else if($hostInfo->name=="roomInfo"){
     $url='panelRoom';
    }
    else{
        $url="panelFacility";
    }
    $this -> renderPartial($url,array('hostInfo'=>  $hostInfo));

  }
  
   public function actionFacility() {
    $this -> layout = 'application.views.layouts.none';
        $pageNo = 1;
    $pageSize=10;
    if(isset($_REQUEST['pageNo'])) {
      $pageNo = $_REQUEST['pageNo'];
    }
    if(isset($_REQUEST['pageSize'])){
      $pageSize=$_REQUEST['pageSize'];
    }
    
    
    $criteria=new CDbCriteria; 
    $facilities=FacilityDetails::model()->findAll('usable=:usable',array(':usable'=>1));
    //echo json_encode($facilities);
    $this -> render('facility',array('facilities'=>$facilities));
      
  }
   
 public function actionShowFacilityDetail(){
    $geometry=$_REQUEST['geometry'];
    $goodsName=$_REQUEST['goodsName'];
    $description=$_REQUEST['description'];
    $this -> renderPartial('showFacilityDetail',array('geometry'=>$geometry,"goodsName"=>$goodsName,"description"=>$description));
  }
    
 public function actionShowFacilityThumb2(){
    $pageNo = 1;
    $pageSize=10;
    if(isset($_REQUEST['pageNo'])) {
      $pageNo = $_REQUEST['pageNo'];
    }
    if(isset($_REQUEST['pageSize'])){
      $pageSize=$_REQUEST['pageSize'];
    }
    // if(isset($_REQUEST['dependId'])) {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
    // } else {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
    // }
    
    //$facilities = FacilityDetails::model() -> getAllFetchPage($pageNo,$pageSize);
    
    $facilities=FacilityDetails::model()->findAll();
    $farray=array();
    foreach( $facilities as $f){
      $fobject = new stdClass();
      $fobject->name=$f->name;
      $fobject->id=$f->id;
      $fobject->gTypeId=$f->gTypeId;
      $fobject->geometryPath=$f->geometryPath;
      $fobject->description=$f->description;
      $fobject->thumbPath=$f->thumbPath;
      $fobject->dependId=$f->dependId;
      $fobject->inputDate=$f->inputDate;
      array_push($farray,$fobject);
    }
    $this -> render('showFacility2',array('facilities'=>$farray));
  }

public function actionShowFacilityThumb(){
    $pageNo = 1;
    $pageSize=10;
    if(isset($_REQUEST['pageNo'])) {
      $pageNo = $_REQUEST['pageNo'];
    }
    if(isset($_REQUEST['pageSize'])){
      $pageSize=$_REQUEST['pageSize'];
    }
    // if(isset($_REQUEST['dependId'])) {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
    // } else {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
    // }
    
    //$facilities = FacilityDetails::model() -> getAllFetchPage($pageNo,$pageSize);
    
    $facilities=FacilityDetails::model()->findAll('usable=:usable',array(':usable'=>1));
    $farray=array();
    foreach( $facilities as $f){
      $fobject = new stdClass();
      $fobject->name=$f->name;
      $fobject->id=$f->id;
      $fobject->gTypeId=$f->gTypeId;
      $fobject->geometryPath=$f->geometryPath;
      $fobject->description=$f->description;
      $fobject->thumbPath=$f->thumbPath;
      $fobject->dependId=$f->dependId;
      $fobject->inputDate=$f->inputDate;
      array_push($farray,$fobject);
    }
    $this -> render('showFacility',array('facilities'=>$farray));
  }
    
    public function actionLoadFacility(){
    $pageNo = 1;
    $pageSize=10;
    if(isset($_REQUEST['pageNo'])) {
      $pageNo = $_REQUEST['pageNo'];
    }
    if(isset($_REQUEST['pageSize'])){
      $pageSize=$_REQUEST['pageSize'];
    }
    // if(isset($_REQUEST['dependId'])) {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdAndDependIdOnPage($_REQUEST['gTypeId'], $_REQUEST['dependId'], $pageNo, 10);
    // } else {
      // $goodsGeo = GoodsGeometry::model() -> getFindBygTypeIdOnPage($_REQUEST['gTypeId'], $pageNo, 10);
    // }
    
    $facilities=FacilityDetails::model()->findAll();
    // print_r($facilities);
    $farray=array();
    foreach( $facilities as $f){
      $fobject = new stdClass();
      $fobject->name=$f->name;
      $fobject->id=$f->id;
      $fobject->gTypeId=$f->gTypeId;
      $fobject->geometryPath=$f->geometryPath;
      $fobject->description=$f->description;
      $fobject->thumbPath=$f->thumbPath;
      $fobject->dependId=$f->dependId;
      $fobject->inputDate=$f->inputDate;
      array_push($farray,$fobject);
    }
    
    echo json_encode($farray);
    
  }
}